import {fetch} from '@/utils/fetch'
import {progress, document, user, answer, comment, task, groupMember} from '../fragment'

export const createByDid = async ({ documentId, groupId = 0, distributeAt = '' }) => {
  let res = await fetch({
    query: `mutation {
      createDocumentProgress (documentId: ${documentId}, groupId: ${groupId}, distributeAt: "${distributeAt}") {
        ${progress}
        document {
          ${document}
          user {
            ${user}
          }
        }
        user {
          ${user}
        }
      }
    }`
  })
  return res.data.createDocumentProgress
}

export const createByTaskId = async (taskId) => {
  let res = await fetch({
    query: `mutation {
      createProgress (taskId: ${taskId}) {
        ${progress}
      }
    }`
  })
  return res.data.createProgress
}

export const getById = async ({progressId, cache = true}) => {
  let res = await fetch({
    query: `query {
      progress (id: ${progressId}) {
        ${progress}
        document {
          ${document}
          user {
            ${user}
          }
        }
        task {
          ${task}
        }
        user {
          ${user}
        }
        member {
          ${groupMember}
        }
        answers {
          ${answer}
          comments {
            ${comment}
          }
          user {
            ${user}
          }
        }
      }
    }`
  })
  return res.data.progress
}

export const createRating = async (progressId) => {
  let res = await fetch({
    query: `mutation {
      createRatingForProgress (input: {
        progressId: ${progressId},
        liked: 1
      }) {
        ${comment}
      }
    }`
  })
  return res.data.createRatingForProgress
}

export const removeRating = async (progressId) => {
  let res = await fetch({
    query: `mutation {
      removeRatingForProgress (input: {
        progressId: ${progressId}
      }) {
        ${comment}
      }
    }`
  })
  return res.data.removeRatingForProgress
}

export const read = async (progressId) => {
  let res = await fetch({
    query: `mutation {
      readProgress (progressId: ${progressId}) {
        ${progress}
        document {
          ${document}
          user {
            ${user}
          }
        }
        task {
          ${task}
        }
        user {
          ${user}
        }
      }
    }`
  })
  return res.data.readProgress
}
